function createFunction(content) {
    let result = document.createElement("div");
    result.innerText = content.text;
    result.classList.add("function");
    result.setAttribute("url", content.url);
    return result;
}

$(() => {
    let functions = [
        {text: "基础数据", url: "base"},
        {text: "采购", url: "purchase"},
        {text: "进仓", url: "entry"},
        {text: "退货", url: "salereturn"},
        {text: '销售', url: 'sale'},
        {text: '出仓', url: 'out'}
    ];

    let $main = {
        box: $(".functions"),
        user: $(".user-button")
    };

    $main.user.text(cookie.find('user') + '(' + cookie.find('power') + ')');

    for (let obj of functions) $main.box.append(createFunction(obj));

    $main.box.on("click", ".function", function () {
        window.open(this.getAttribute("url"), "_self");
    });
});

//search
$(() => {
    let searchInput = $('.search-input'),
        searchButton = $('.search-submit');

    let targets = [
        {target:'outStorage', search: 'outStorageNo', type: true},
        {target:'storages', search: 'str', type: false},
        {target: 'enterStorage', search: 'entryStorageNo', type: true},
        {target: 'returnMaster', search: 'returnNo', type: true},
        {target: 'purchase', search:'orderNo', type: false},
        {target: 'orderMaster', search: 'orderNo', type: false}
    ],
        result = {},
        searchBox = $('.search-result-box'),
        search = (value, target) => {
            let data = '&pageNum=1&pageSize=100&'+target['search']+'='+value;
            if (target['type']) data += '&type=0';
            if (target['type']) {
                $.ajax({
                    url: 'http://www.fjemiot.cn/erp/'+target['target'],
                    data: data,
                    success: (reply) => {
                        result[target['target']] = reply['value'];
                        $.ajax({
                            url: 'http://www.fjemiot.cn/erp/'+target['target'],
                            data: 'type=1&pageNum=1&pageSize=100&'+target['search']+'='+value,
                            success: (reply) => {
                                result[target['target']].concat(reply['value']);
                                let newResult = document.createElement('div');
                                newResult.classList.add('model');
                                newResult.append(tablegen(result[target['target']]));
                                searchBox.append(newResult);
                            }
                        });
                    }
                });

            } else {
                $.ajax({
                    url: 'http://www.fjemiot.cn/erp/'+target['target'],
                    data: 'pageNum=1&pageSize=100&'+target['search']+'='+value,
                    success: (reply) => {
                        result[target['target']] = reply['value'];
                        let newResult = document.createElement('div');
                        newResult.classList.add('model');
                        newResult.append(tablegen(result[target['target']]));
                        searchBox.append(newResult);
                    }
                });
            }
        };

    searchButton.click(() => {
        let message = searchInput.val().trim();
        if (message != '') {
            result = {};
            searchBox.html('');
            for (let t of targets) {
                search(message, t);
            }
            searchBox.removeClass('hide').addClass('show');
        } else {
            searchInput.val('');
        }
    });

    searchInput.keydown((e) => {
        if (e.key == 'Enter') {
            searchButton.click();
        }
    });

    $(document.body).on('click', function (e) {
        if ($(e.target).parents('.search-result-box').length == 0
        && e.target.tagName != 'BUTTON') {
            searchBox.removeClass('show').addClass('hide');
        }
    });
});
